package com.ge.pp;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Vector;

class ThresholdFilter extends ImageFilter {
	FilterAttribute threshold;
	public ThresholdFilter(PicturePane p, float _threshold) {
		super(p);
		name = "Threshold Filter";
		threshold = new FilterAttribute("Threshold", 0, 255, _threshold);			
		attributes = new Vector<FilterAttribute>();
		attributes.add(threshold);
	}
	
	public BufferedImage filter(BufferedImage img) throws Exception {
		
		try
		{BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
		Raster inRaster = img.getRaster();		
		WritableRaster r = newImg.getRaster();
		
		float [] f = new float[3];
		float t = threshold.val;			
		
		for(int x=0; x<r.getWidth(); x++) {
			for (int y=0; y<r.getHeight(); y++) {
				inRaster.getPixel(x, y, f);
						
				if (f[0] < t && f[1] < t && f[2] < t) {				 
					f[0] = 0; f[1] =0; f[2] = 0;
				} 				
					
				r.setPixel(x,y, f);
			}
		}			

		return newImg;
		}
		catch(Exception e)
		{
			throw e;
		}
	}
}